package xyz.yuzegod.megawalls.inventory;

import fr.minuskube.inv.ClickableItem;
import fr.minuskube.inv.SmartInventory;
import fr.minuskube.inv.content.InventoryContents;
import fr.minuskube.inv.content.InventoryProvider;
import fr.minuskube.inv.content.SlotIterator;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.game.Game;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.game.GameTeam;
import xyz.yuzegod.megawalls.game.GameType;
import xyz.yuzegod.megawalls.spectator.SpectatorSettings;
import xyz.yuzegod.megawalls.stats.EffectStatsContainer;
import xyz.yuzegod.megawalls.util.ItemBuilder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SpectatorInventory {
     public static SmartInventory buildSettings() {
          SmartInventory.Builder builder = SmartInventory.builder(MegaWalls.getInstance().getInventoryManager());
          builder.title("旁观者设置");
          builder.type(InventoryType.CHEST);
          builder.size(4, 9);
          builder.closeable(true);
          builder.provider(new InventoryProvider() {
               public void init(Player player, InventoryContents contents) {
                    GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
                    SpectatorSettings settings = SpectatorSettings.get(gamePlayer);
                    contents.set(1, 2, ClickableItem.of((new ItemBuilder(Material.LEATHER_BOOTS)).setDisplayName("§a没有速度效果").build(), (e) -> {
                         if (settings.getSpeed() == 0) {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                         } else {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.sendMessage("§c你不再有任何速度效果！");
                              settings.setSpeed(0);
                              contents.inventory().open(player);
                         }
                    }));
                    contents.set(1, 3, ClickableItem.of((new ItemBuilder(Material.CHAINMAIL_BOOTS)).setDisplayName("§a速度 I").build(), (e) -> {
                         if (settings.getSpeed() == 1) {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 0));
                         } else {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 0));
                              player.sendMessage("§a你获得了 速度 I 效果！");
                              settings.setSpeed(1);
                              contents.inventory().open(player);
                         }
                    }));
                    contents.set(1, 4, ClickableItem.of((new ItemBuilder(Material.IRON_BOOTS)).setDisplayName("§a速度 II").build(), (e) -> {
                         if (settings.getSpeed() == 2) {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 1));
                         } else {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 1));
                              player.sendMessage("§a你获得了 速度 II 效果！");
                              settings.setSpeed(2);
                              contents.inventory().open(player);
                         }
                    }));
                    contents.set(1, 5, ClickableItem.of((new ItemBuilder(Material.GOLD_BOOTS)).setDisplayName("§a速度 III").build(), (e) -> {
                         if (settings.getSpeed() == 3) {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 2));
                         } else {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 2));
                              player.sendMessage("§a你获得了 速度 III 效果！");
                              settings.setSpeed(3);
                              contents.inventory().open(player);
                         }
                    }));
                    contents.set(1, 6, ClickableItem.of((new ItemBuilder(Material.DIAMOND_BOOTS)).setDisplayName("§a速度 IV").build(), (e) -> {
                         if (settings.getSpeed() == 4) {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 3));
                         } else {
                              if (player.hasPotionEffect(PotionEffectType.SPEED)) {
                                   player.removePotionEffect(PotionEffectType.SPEED);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 3));
                              player.sendMessage("§a你获得了 速度 IV 效果！");
                              settings.setSpeed(4);
                              contents.inventory().open(player);
                         }
                    }));
                    if (settings.getOption(SpectatorSettings.Option.AUTOTP)) {
                         contents.set(2, 2, ClickableItem.of((new ItemBuilder(Material.COMPASS)).setDisplayName("§c停用自动传送").setLore("§7点击停用自动传送").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.AUTOTP, false);
                              player.sendMessage("§c你不再被自动传送到目标位置！");
                              contents.inventory().open(player);
                         }));
                    } else {
                         contents.set(2, 2, ClickableItem.of((new ItemBuilder(Material.COMPASS)).setDisplayName("§a启动自动传送").setLore("§7点击启用自动传送").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.AUTOTP, true);
                              player.sendMessage("§a你开启了自动传送功能！");
                              contents.inventory().open(player);
                         }));
                    }

                    if (settings.getOption(SpectatorSettings.Option.NIGHTVISION)) {
                         contents.set(2, 3, ClickableItem.of((new ItemBuilder(Material.ENDER_PEARL)).setDisplayName("§c停用夜视").setLore("§7点击停用夜视").build(), (e) -> {
                              if (player.hasPotionEffect(PotionEffectType.NIGHT_VISION)) {
                                   player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                              }

                              settings.setOption(SpectatorSettings.Option.NIGHTVISION, false);
                              player.sendMessage("§c你不再有夜视效果了！");
                              contents.inventory().open(player);
                         }));
                    } else {
                         contents.set(2, 3, ClickableItem.of((new ItemBuilder(Material.EYE_OF_ENDER)).setDisplayName("§a启动夜视").setLore("§7点击启用夜视").build(), (e) -> {
                              if (player.hasPotionEffect(PotionEffectType.NIGHT_VISION)) {
                                   player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                              }

                              player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 1));
                              settings.setOption(SpectatorSettings.Option.NIGHTVISION, true);
                              player.sendMessage("§a你现在拥有了夜视！");
                              contents.inventory().open(player);
                         }));
                    }

                    if (settings.getOption(SpectatorSettings.Option.FIRSTPERSON)) {
                         contents.set(2, 4, ClickableItem.of((new ItemBuilder(Material.WATCH)).setDisplayName("§c停用第一人称旁观").setLore("§7点击停用第一人称旁观").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.FIRSTPERSON, false);
                              player.sendMessage("§c你将默认使用第三人称模式！");
                              if (gamePlayer.isSpectator() && player.getGameMode() == GameMode.SPECTATOR) {
                                   gamePlayer.sendTitle("§e退出旁观模式", "", 0, 20, 0);
                                   player.setGameMode(GameMode.ADVENTURE);
                                   player.setAllowFlight(true);
                                   player.setFlying(true);
                              }

                              contents.inventory().open(player);
                         }));
                    } else {
                         contents.set(2, 4, ClickableItem.of((new ItemBuilder(Material.WATCH)).setDisplayName("§a启动第一人称旁观").setLore("§7点击确认使用指南针时", "§7自动沿用第一人称旁观！", "§7你也可以右键点击一位玩家", "§7来启用第一人称旁观").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.FIRSTPERSON, true);
                              player.sendMessage("§a当你用你的指南针现在一个玩家后，你会被自动传送到他那里！");
                              contents.inventory().open(player);
                         }));
                    }

                    if (settings.getOption(SpectatorSettings.Option.HIDEOTHER)) {
                         contents.set(2, 5, ClickableItem.of((new ItemBuilder(Material.GLOWSTONE_DUST)).setDisplayName("§a查看旁观者").setLore("§7点击以显示其他旁观者").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.HIDEOTHER, false);
                              player.sendMessage("§a你现在可以看见其他旁观者了！");
                              contents.inventory().open(player);
                         }));
                    } else {
                         contents.set(2, 5, ClickableItem.of((new ItemBuilder(Material.REDSTONE)).setDisplayName("§c隐藏旁观者").setLore("§7点击来隐藏其他旁观者").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.HIDEOTHER, true);
                              player.sendMessage("§c你不会再看到其他的旁观者！");
                              contents.inventory().open(player);
                         }));
                    }

                    if (settings.getOption(SpectatorSettings.Option.FLY)) {
                         contents.set(2, 6, ClickableItem.of((new ItemBuilder(Material.FEATHER)).setDisplayName("§c停用持续飞行").setLore("§7点击停用飞行").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.FLY, false);
                              player.sendMessage("§a你现在能停止飞行！");
                              contents.inventory().open(player);
                         }));
                    } else {
                         contents.set(2, 6, ClickableItem.of((new ItemBuilder(Material.FEATHER)).setDisplayName("§a启动持续飞行").setLore("§7点击启用飞行").build(), (e) -> {
                              settings.setOption(SpectatorSettings.Option.FLY, true);
                              player.sendMessage("§a你现在不能停止飞行！");
                              if (player.isOnGround()) {
                                   player.getLocation().setY(player.getLocation().getY() + 0.1D);
                              }

                              player.setAllowFlight(true);
                              player.setFlying(true);
                              contents.inventory().open(player);
                         }));
                    }

               }

               public void update(Player player, InventoryContents contents) {
               }
          });
          return builder.build();
     }

     public static SmartInventory alivePlayers(final List players) {
          SmartInventory.Builder builder = SmartInventory.builder(MegaWalls.getInstance().getInventoryManager());
          builder.title("传送器");
          builder.type(InventoryType.CHEST);
          builder.size(5, 9);
          builder.closeable(true);
          builder.provider(new InventoryProvider() {
               SlotIterator.Impl slotIterator = null;

               public void init(Player player, InventoryContents contents) {
                    Game game = MegaWalls.getInstance().getGame();
                    EffectStatsContainer effectStats = GamePlayer.get(player.getUniqueId()).getPlayerStats().getEffectStats();
                    contents.pagination().setItemsPerPage(28);
                    List items = new ArrayList();
                    Iterator var6 = players.iterator();

                    while(var6.hasNext()) {
                         GamePlayer gamePlayer = (GamePlayer)var6.next();
                         ItemBuilder itemBuilder = (new ItemBuilder(Material.SKULL_ITEM, 1, (byte)3)).setDisplayName("§7" + gamePlayer.getPlayer().getDisplayName()).setSkullOwner(gamePlayer.getName());
                         List lore = new ArrayList();
                         lore.add(effectStats.getColor(gamePlayer.getGameTeam().getTeamColor()).getChatColor() + gamePlayer.getGameTeam().getTeamColor().getText() + "队");
                         lore.add(" ");
                         lore.add("§7血量: §f" + (int)gamePlayer.getPlayer().getHealth() + " §c❤");
                         lore.add("§7职业: §f" + gamePlayer.getPlayerStats().getSelected().getDisplayName());
                         lore.add(" ");
                         lore.add("§7点击旁观！");
                         itemBuilder.setLore((List)lore);
                         items.add(ClickableItem.of(itemBuilder.build(), (e) -> {
                              GamePlayer.get(player.getUniqueId()).getSpectatorTarget().setTarget(gamePlayer);
                              GamePlayer.get(player.getUniqueId()).getSpectatorTarget().tp();
                              player.closeInventory();
                         }));
                    }

                    contents.pagination().setItems((ClickableItem[])items.toArray(new ClickableItem[0]));
                    this.slotIterator = new SlotIterator.Impl(contents, contents.inventory(), SlotIterator.Type.HORIZONTAL, 0, 1);
                    this.slotIterator.allowOverride(true);

                    int slot;
                    for(slot = 0; slot <= 3; ++slot) {
                         this.slotIterator.blacklist(slot, 0);
                         this.slotIterator.blacklist(slot, 8);
                    }

                    for(slot = 0; slot <= 8; ++slot) {
                         this.slotIterator.blacklist(4, slot);
                    }

                    ClickableItem[] var12 = contents.pagination().getPageItems();
                    int var11 = var12.length;

                    for(int var14 = 0; var14 < var11; ++var14) {
                         ClickableItem item = var12[var14];
                         this.slotIterator.next().set(item);
                         if (this.slotIterator.ended()) {
                              break;
                         }
                    }

                    this.update(player, contents);
                    slot = game.getGameType() == GameType.NORMAL ? 2 : 3;

                    for(Iterator var13 = game.getTeams().iterator(); var13.hasNext(); ++slot) {
                         GameTeam gameTeam = (GameTeam)var13.next();
                         String name = effectStats.getColor(gameTeam.getTeamColor()).getChatColor() + gameTeam.getTeamColor().getText() + "队";
                         contents.set(4, slot, ClickableItem.of((new ItemBuilder(Material.WOOL)).setDisplayName(name).setLore("§7点此以仅显示" + name + "§7！").setDyeColor(gameTeam.getTeamColor().getDyeColor()).build(), (e) -> {
                              SpectatorInventory.alivePlayers(gameTeam.getAlivePlayers()).open(player);
                         }));
                    }

                    contents.set(4, slot, ClickableItem.of((new ItemBuilder(Material.GLASS)).setDisplayName("§f所有玩家").setLore("§7点此以仅显示所有玩家！").build(), (e) -> {
                         SpectatorInventory.alivePlayers(MegaWalls.getIngame()).open(player);
                    }));
               }

               public void update(Player player, InventoryContents contents) {
                    boolean onlyOnePage = contents.pagination().isFirst() && contents.pagination().isLast();
                    if (!contents.pagination().isFirst() && !onlyOnePage) {
                         contents.set(2, 0, ClickableItem.of((new ItemBuilder(Material.ARROW)).setDisplayName("§a上一页").build(), (e) -> {
                              contents.pagination().previous();
                              this.slotIterator.row(0).column(1);
                              ClickableItem[] var4 = contents.pagination().getPageItems();
                              int var5 = var4.length;

                              for(int var6 = 0; var6 < var5; ++var6) {
                                   ClickableItem item = var4[var6];
                                   this.slotIterator.next().set(item);
                                   if (this.slotIterator.ended()) {
                                        break;
                                   }
                              }

                              this.update(player, contents);
                         }));
                    } else {
                         contents.set(2, 0, (ClickableItem)null);
                    }

                    if (!contents.pagination().isLast() && !onlyOnePage) {
                         contents.set(2, 8, ClickableItem.of((new ItemBuilder(Material.ARROW)).setDisplayName("§a下一页").build(), (e) -> {
                              contents.pagination().next();
                              this.slotIterator.row(0).column(1);
                              ClickableItem[] var4 = contents.pagination().getPageItems();
                              int var5 = var4.length;

                              for(int var6 = 0; var6 < var5; ++var6) {
                                   ClickableItem item = var4[var6];
                                   this.slotIterator.next().set(item);
                                   if (this.slotIterator.ended()) {
                                        break;
                                   }
                              }

                              this.update(player, contents);
                         }));
                    } else {
                         contents.set(2, 8, (ClickableItem)null);
                    }

               }
          });
          return builder.build();
     }
}
